//-------------------------------------------------------------------
//	Copyright (c) 2010-2011  Zhirnov Andrey
//	This file is part of the "UXGEN_ResourceConverter" project.
//	See copyright notice in "Converter.h".
//-------------------------------------------------------------------

#include "MeshConverters.h"
#include "..\\..\\Core\\CConverter.h"


bool CMDLConverter::ConvertMesh(TModelInfo &sInfo)
{
	TModelData::TModelMesh	s_model( String::GetFileName( sInfo.pFileName ) );
	IFilePtr				p_file;
	e_model_component::type	e_comp;
	bool					ret = false;
	
	if ( CORE->LoadFile( sInfo.pFileName, p_file, e_file_open_flags::DEF_READ ) != S_OK )
		return false;

	if ( !Loaders::LoadModel( p_file, &s_model.sMesh, nullptr, nullptr, nullptr, &e_comp ) )
		return false;


	if ( EnumCmpE( e_comp, e_model_component::MESH ) ||
		 EnumCmpE( e_comp, e_model_component::MULTI_MESH ) )
	{
		sInfo.sData.aMeshes.PushBack( s_model );
		ret = true;
	}

	return ret;
}


bool CMDLConverter::ConvertFrameAnim(TModelInfo &sInfo)
{
	TModelData::TModelMesh	s_model( String::GetFileName( sInfo.pFileName ) );
	TMeshAnimation			s_anim;
	IFilePtr				p_file;
	e_model_component::type	e_comp;
	bool					ret = false;
	
	if ( CORE->LoadFile( sInfo.pFileName, p_file, e_file_open_flags::DEF_READ ) != S_OK )
		return false;

	if ( !Loaders::LoadModel( p_file, &s_model.sMesh, &s_anim, nullptr, nullptr, &e_comp ) )
		return false;


	if ( EnumCmpE( e_comp, e_model_component::FRAME_ANIM ) )
	{
		s_model.uAnimationIdx = (uint)sInfo.sData.aAnimations.Count();
		sInfo.sData.aAnimations.PushBack( TModelData::TMeshAnimInfo( sInfo.pFileName ) );
		sInfo.sData.aAnimations.Back().sAnim.Swap( s_anim );
		ret = true;
	}

	if ( EnumCmpE( e_comp, e_model_component::MESH ) ||
		 EnumCmpE( e_comp, e_model_component::MULTI_MESH ) )
	{
		sInfo.sData.aMeshes.PushBack( s_model );
		ret = true;
	}

	return ret;
}


bool CMDLConverter::ConvertSceletal(TModelInfo &sInfo)
{
	TModelData::TModelMesh	s_model( String::GetFileName( sInfo.pFileName ) );
	TMeshAnimation			s_anim;
	IFilePtr				p_file;
	e_model_component::type	e_comp;
	bool					ret = false;
	
	if ( CORE->LoadFile( sInfo.pFileName, p_file, e_file_open_flags::DEF_READ ) != S_OK )
		return false;

	if ( !Loaders::LoadModel( p_file, &s_model.sMesh, &s_anim, nullptr, nullptr, &e_comp ) )
		return false;


	if ( EnumCmpE( e_comp, e_model_component::SCELETAL_ANIM ) )
	{
		s_model.uAnimationIdx = (uint)sInfo.sData.aAnimations.Count();
		sInfo.sData.aAnimations.PushBack( TModelData::TMeshAnimInfo( sInfo.pFileName ) );
		sInfo.sData.aAnimations.Back().sAnim.Swap( s_anim );
		ret = true;
	}

	if ( EnumCmpE( e_comp, e_model_component::SCELETAL_MULTI_MESH ) )
	{
		sInfo.sData.aMeshes.PushBack( s_model );
		ret = true;
	}

	return ret;
}


bool CMDLConverter::ConvertAutoType(TModelInfo &sInfo)
{
	TModelData::TModelMesh	s_model( String::GetFileName( sInfo.pFileName ) );
	TMeshAnimation			s_anim;
	TMeshMaterial			s_material;
	TMeshPhysics			s_physics;
	IFilePtr				p_file;
	e_model_component::type	e_comp;
	bool					ret = false;

	if ( CORE->LoadFile( sInfo.pFileName, p_file, e_file_open_flags::DEF_READ ) != S_OK )
		return false;

	if ( !Loaders::LoadModel( p_file, &s_model.sMesh, &s_anim, &s_material, &s_physics, &e_comp ) )
		return false;


	if ( EnumCmp( e_comp, e_model_component::_ANIMATION_MASK ) )
	{
		s_model.uAnimationIdx = (uint)sInfo.sData.aAnimations.Count();
		sInfo.sData.aAnimations.PushBack( TModelData::TMeshAnimInfo( sInfo.pFileName ) );
		sInfo.sData.aAnimations.Back().sAnim.Swap( s_anim );
		ret = true;
	}

	if ( EnumCmp( e_comp, e_model_component::_MATERIAL_MASK ) )
	{
		s_model.uMaterialIdx = (uint)sInfo.sData.aMaterials.Count();
		sInfo.sData.aMaterials.PushBack( TModelData::TMeshMtrInfo( sInfo.pFileName ) );
		sInfo.sData.aMaterials.Back().sMaterial.Swap( s_material );
		ret = true;
	}

	if ( EnumCmp( e_comp, e_model_component::_PHYSICS_MASK ) )
	{
		s_model.uPhysicsIdx = (uint)sInfo.sData.aPhysics.Count();
		sInfo.sData.aPhysics.PushBack( TModelData::TMeshPhysInfo( sInfo.pFileName ) );
		sInfo.sData.aPhysics.Back().sPhys.Swap( s_physics );
		ret = true;
	}

	if ( EnumCmp( e_comp, e_model_component::_GEOMETRY_MASK ) )
	{
		sInfo.sData.aMeshes.PushBack( s_model );
		ret = true;
	}

	return ret;
}
